package com.gouaixin.bee.beeframework2.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.image.AbImageLoader;
import com.ab.util.AbImageUtil;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.extra.im.ChatData;
import com.gouaixin.bee.beeframework2.bean.getChatFriendsResponse;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public class MailAdapter extends BaseAdapter {

    List<List<ChatData>> liaotian;


    Context context;
    //	ArrayList<HashMap<String, Object>> al;
    LayoutInflater mInflater;
    ArrayList<getChatFriendsResponse> datas=new ArrayList<getChatFriendsResponse>();

    AbImageLoader mAbImageLoader;

    String hericon="";

    int qian=4;//固定显示的图标个数

    public MailAdapter(Context context) {
        this.context = context;
//		this.al = al;
        mInflater = LayoutInflater.from(context);
        mAbImageLoader=AbImageLoader.newInstance(context);


    }


    public MailAdapter(Context context, ArrayList<HashMap<String, Object>> al) {
        this.context = context;
//		this.al = al;
        mInflater = LayoutInflater.from(context);

    }

    public void setData(ArrayList<getChatFriendsResponse> datas){
        this.datas=datas;
    }


    public int getCount() {
        if(datas.isEmpty()){
            return qian;
        }else {
            return qian+datas.size();
        }

    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }




    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;
//
//		if(convertView == null){

        if(position==0){
            convertView = mInflater.inflate(R.layout.mail_item00, null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);

            viewHolder.numBg = (LinearLayout) convertView.findViewById(R.id.mail_num_bg);
            viewHolder.num = (TextView) convertView.findViewById(R.id.mail_num);

            //6.配置数字相关
            String cid= PreferencesUtils.getString(context, "cid");
            int temp= PreferencesUtils.getInt(context, cid + "Fangke", 0);
            mLog.e(cid + "Fangke=" + temp);
            if(temp==0){
                viewHolder.numBg.setVisibility(View.GONE);
            }else {
                viewHolder.numBg.setVisibility(View.VISIBLE);
                viewHolder.num.setText(temp+"");
            }


        }else if (position==1){
            convertView = mInflater.inflate(R.layout.mail_item01, null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);
        }else if (position==2){
            convertView = mInflater.inflate(R.layout.mail_item02, null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);

            viewHolder.numBg = (LinearLayout) convertView.findViewById(R.id.mail_num_bg);
            viewHolder.num = (TextView) convertView.findViewById(R.id.mail_num);

            //6.配置数字相关
            String cid=PreferencesUtils.getString(context,"cid");
            int temp= PreferencesUtils.getInt(context, cid + "ZhenxinhuaFriend", 0);
            mLog.e("添加真心话好友的方法(适配器中)：newZhenxinhuaFriendNumer=" + temp);
            if(temp==0){
                viewHolder.numBg.setVisibility(View.GONE);
            }else {
                viewHolder.numBg.setVisibility(View.VISIBLE);
                viewHolder.num.setText(temp+"");
            }
        }else if (position==3){
            convertView = mInflater.inflate(R.layout.mail_item03, null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);

            viewHolder.numBg = (LinearLayout) convertView.findViewById(R.id.mail_num_bg);
            viewHolder.num = (TextView) convertView.findViewById(R.id.mail_num);

            //6.配置数字相关
            String cid=PreferencesUtils.getString(context,"cid");
            int temp= PreferencesUtils.getInt(context,"MishuMsg", 0);
            mLog.e("缘分小秘书消息数量(适配器中)：MishuMsg=" + temp);
            Log.e("test", "适配器中接受到的数量：MishuMsg=" + temp);

            if(temp==0){
                viewHolder.numBg.setVisibility(View.GONE);
            }else {
                viewHolder.numBg.setVisibility(View.VISIBLE);
                viewHolder.num.setText(temp+"");
            }
        }else{
            convertView = mInflater.inflate(R.layout.mail_item, null);
            viewHolder = new ViewHolder();

            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.numBg = (LinearLayout) convertView.findViewById(R.id.mail_num_bg);
            viewHolder.num = (TextView) convertView.findViewById(R.id.mail_num);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.text = (TextView) convertView.findViewById(R.id.text);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);

            getChatFriendsResponse data=datas.get(position-qian);
            viewHolder.name.setText(data.getName());
            viewHolder.time.setText(data.getTime());
            viewHolder.text.setText(data.getText());

            //6.配置数字相关
            String jid=data.getId();
            int temp= PreferencesUtils.getInt(context, jid + "MailNumber", 0);
//                mLog.e(jid + "MailNumber="+temp);
            if(temp==0){
                viewHolder.numBg.setVisibility(View.GONE);
            }else {
                viewHolder.numBg.setVisibility(View.VISIBLE);
                viewHolder.num.setText(temp+"");
            }



//                viewHolder.text.setText("快点去和我聊天吧！");

//                mAbImageLoader.display(viewHolder.icon,data.getIcon());

            hericon=data.getIcon();

            if(!hericon.equals("")){

                ImageLoader imageLoader=ImageLoader.getInstance();

                imageLoader.loadImage(hericon,new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {

                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                        viewHolder.icon.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(bitmap));

                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });
//                    imageLoader.displayImage(hericon,viewHolder.icon);
//                    AbImageLoader.newInstance(context).display(viewHolder.icon,hericon);
            }


//                int number=Integer.parseInt(data.getNumber());
//
//                if(number==0){
//                    viewHolder.numBg.setVisibility(View.GONE);
//                }else{
//                    viewHolder.numBg.setVisibility(View.VISIBLE);
//                    viewHolder.num.setText(number+"");
//                }
            convertView.setTag(viewHolder);
        }

//		}else {
//
//			viewHolder = (ViewHolder) convertView.getTag();
//		}




        return convertView;
    }
    /**
     * 视图控制类
     */
    class ViewHolder {
        ImageView icon;
        LinearLayout numBg;
        TextView num;
        TextView name;
        TextView text;
        TextView time;
    }





}
